package com.ives.launcher.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ives.launcher.model.ItemInfo;
import com.ives.launcher.model.ItemInfo_App;
import com.ives.launcher.model.ItemInfo_Floder;
import com.ives.launcher.util.ImageTool;

/**
 * 每个页面中的item（包括文件夹，快捷方式，启动图标等）
 * @author ives
 *
 */
@SuppressLint("ParserError")
public class View_Cell extends View  {
	private ItemInfo itemInfo;//所附加的信息(包括文件夹，快捷方式，启动图标等)
	private int[] startPoint = new int[2];//用于记录触点与x轴和y轴的距离
	private String title="临时的";//用于显示的标题
	private Bitmap icon;//用于显示的图标(如果是文件夹，那么次图为几个图标合成的一张图)
	private float leftGap = 3;//单格左边的间距
	private float topGap = 3;//单格上方的间距
	private boolean isDrawTitle = true;//是否显示标题
	MOnClickLinstener mOnClickLinstener;//自定义对点击事件
	TextPaint paint = new TextPaint();//画笔
	public static View_Cell clickingView = null;//正在被点击的cell
	
	
	private boolean isClicking = false;//是否正在被点击
	/**constructor**/
	private View_Cell(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	private View_Cell(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}
	public View_Cell(View_Cell source){
		super(source.getContext());
		if(source.itemInfo!=null)
		this.itemInfo = source.itemInfo;
		this.startPoint = source.startPoint;
		this.title = source.title;
		this.icon = source.icon;
		this.leftGap = source.leftGap;
		this.topGap = source.topGap;
		this.isDrawTitle = source.isDrawTitle;
		this.mOnClickLinstener = source.mOnClickLinstener;
		this.setLayoutParams(new View_Page_Item.LayoutParams(source.getLayoutParams()));
	}
	public View_Cell(Context context) {
		this(context,null,0);
	}
	public View_Cell(Context context, String title, Bitmap icon,float leftGap, float topGap) {
		this(context);
		this.title = title;
		this.icon = icon;
		this.leftGap = leftGap;
		this.topGap = topGap;
	}
	
	/**
	 * 外部可以通过此方法间接地调用setMeasuredDimension方法
	 * @param width
	 * @param height
	 */
	public void setMeasureSize(int width,int height){
		this.setMeasuredDimension(width, height);
		this.postInvalidate();
		
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(icon==null){
			return;
		}
		if(isClicking){
			paint.setColor(Color.YELLOW);
			paint.setAlpha(155);
		}else{
			paint.setColor(Color.WHITE);
			paint.setAlpha(255);
		}
		if(isDrawTitle){//显示标题
			Bitmap bitmap = ImageTool.getScaleBitmap(icon, getWidth()-leftGap*2, getWidth()-leftGap*2);
			canvas.drawBitmap(bitmap, leftGap, topGap, paint);
			
			paint.setStrokeWidth(5);
			paint.setTextSize(18f);//设置字体大小
			StaticLayout layout = new StaticLayout(title,paint,(int) getWidth(),Alignment.ALIGN_CENTER,0.85F,0,false);
			canvas.translate(0,getWidth()-leftGap*2+2);
			layout.draw(canvas);
		}else{//不显示标题
			Bitmap bitmap = ImageTool.getScaleBitmap(icon, getWidth()-leftGap*2, getWidth()-leftGap*2);
			canvas.drawBitmap(bitmap, leftGap, (getHeight()-bitmap.getHeight())/2, paint);
		}
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//记录下被按的cell，如果在容器的onclick事件中触发
		if(MotionEvent.ACTION_DOWN==event.getAction()){
			clickingView = this;
			isClicking = true;
			postInvalidate();
		}else if(MotionEvent.ACTION_UP==event.getAction() || MotionEvent.ACTION_OUTSIDE==event.getAction() || MotionEvent.ACTION_CANCEL==event.getAction()){
			isClicking = false;
		}
		return false;
	}
	
	
	
	/**
	 * 自定义的点击事件
	 * @author ives
	 *
	 */
	public interface MOnClickLinstener{
		void onClick();
	}
	/**getter and setter**/
	public int[] getStartPoint() {
		return startPoint;
	}

	public void setStartPoint(int[] startPoint) {
		this.startPoint = startPoint;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Bitmap getIcon() {
		return icon;
	}

	public void setIcon(Bitmap icon) {
		this.icon = icon;
	}

	public float getLeftGap() {
		return leftGap;
	}

	public void setLeftGap(float leftGap) {
		this.leftGap = leftGap;
	}

	public float getTopGap() {
		return topGap;
	}

	public void setTopGap(float topGap) {
		this.topGap = topGap;
	}

	public boolean isDrawTitle() {
		return isDrawTitle;
	}

	public void setDrawTitle(boolean isDrawTitle) {
		this.isDrawTitle = isDrawTitle;
	}

	public TextPaint getPaint() {
		return paint;
	}

	public void setPaint(TextPaint paint) {
		this.paint = paint;
	}

	public ItemInfo getItemInfo() {
		return itemInfo;
	}

	public void setItemInfo(ItemInfo itemInfo) {
		this.itemInfo = itemInfo;
	}

	public MOnClickLinstener getmOnClickLinstener() {
		return mOnClickLinstener;
	}

	public void setmOnClickLinstener(MOnClickLinstener mOnClickLinstener) {
		this.mOnClickLinstener = mOnClickLinstener;
	}


	public boolean isClicking() {
		return isClicking;
	}


	public void setClicking(boolean isClicking) {
		this.isClicking = isClicking;
	}

	
}
